package abq2.jic.jic.plugin.operate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class ScoreTask implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_Stu_Score".equalsIgnoreCase(action)) {
            //获取考试的名称
            DynamicObject myStuInfo = BusinessDataServiceHelper.loadSingle("abq2_tk_student_info",
                    "creator,abq2_textfield,abq2_textfield8,abq2_textfield5"
                    , new QFilter[]{new QFilter("creator", QCP.equals, RequestContext.get().getCurrUserId())});

            String stu_name = myStuInfo.getString("creator.name");
            String exam_name = params.get("exam_name");
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_int",
                    "abq2_billnofield," +
                            "abq2_textfield5," +          //学校
                            "abq2_textfield2," +            //学生的班级
                            "stu_name," +     //学生姓名
                            "abq2_textfield3," +      //学生学号
                            "abq2_decimalfield1111," +   //学生成绩总分
                            "abq2_textfield8," +     //考试名称
                            //把单据体和单据体里的字段加进load里面
                            "abq2_entryentity," + //科目成绩的单据体
                            "abq2_textfield," +   //考试科目
                            "abq2_decimalfield111," +   //科目对应的成绩
                            "abq2_daterangefield_startdate",           //考试日期
                    (new QFilter("stu_name", QCP.is_notnull, null)).toArray());

            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                //将学生的考试基本的信息加入JSONArray
                if ( stu_name.contains(dynamicObject.getString("stu_name")) && exam_name.contains(dynamicObject.getString("abq2_textfield8"))) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stu_name", dynamicObject.getString("stu_name"));
                    jsonObject.put("school_name", dynamicObject.getString("abq2_textfield5"));
                    jsonObject.put("stu_id", dynamicObject.getString("abq2_textfield3"));
                    jsonObject.put("class_name", dynamicObject.getString("abq2_textfield2"));
                    jsonObject.put("exam_time", new SimpleDateFormat("yyyy-MM-dd").format(dynamicObject.getDate("abq2_daterangefield_startdate")));
                    jsonObject.put("exam_name", dynamicObject.getString("abq2_textfield8"));
                    jsonObject.put("exam_all_score", dynamicObject.getBigDecimal("abq2_decimalfield1111"));
                    jsonArray.add(jsonObject);
                    //获取学生的成绩信息中的单据体信息，获取学生的各门科目的成绩信息
                    //先把单据体的collection创建出来，再获取这个单据体的collection的字段（按照字段名），如下
                    DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("abq2_entryentity");
                    for (DynamicObject dynamicObject2 : dynamicObjectCollection) { //遍历分录数据
                        //将各科目的的信息加入JSONArray
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("subject", dynamicObject2.getString("abq2_textfield"));  //考试科目
                        jsonObject2.put("sub_score", dynamicObject2.getBigDecimal("abq2_decimalfield111"));  //考试成绩
                        jsonArray.add(jsonObject2);
                    }
                }
            }


            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}
